//
// Created by sukai on 22-9-19.
//

#include <iostream>
#include <ros/ros.h>
#include <signal.h>
#include "contnav_msgs/Dictionaries.h"
#include "contnav_msgs/DictionariesVector.h"
#include "contnav_srvs/TABLE_CONTORLLER.h"
#include "common.h"

using namespace std;
/**
 *
 * @param argc
 * @param argv
 * @return
 * 测试节点
 * 2022-09-19
 * sukai
 * rosrun contnav testSqlite_node
 */




//数据库操作
ros::ServiceClient   SqlOperationClient;
//字典数据
map<string, string> dictionariesMap_;

//ctrl + c
void mySigintHandler(int sig)
{
    std::cout << "ctrl c  sig: "<<sig<<std::endl;

    ros::shutdown();
    if (ros::isShuttingDown()){
        cout<<"close ros  test.cpp"<<endl;
    }
}



//sql where 条件
void whereSql(std::string Condition,std::string Key_i,std::string Symbol,std::string Value,contnav_srvs::TABLE_CONTORLLER &tableContorllerService){

    //1. where 条件
    contnav_msgs::Dictionaries dictionaries;
    dictionaries.Condition=Condition;
    dictionaries.Key_i=Key_i;
    dictionaries.Symbol=Symbol;
    dictionaries.Value=Value;
    tableContorllerService.request.request_where_dictionariesVector.DictionariesVectors.push_back(dictionaries);

}



//封装条件数据
void mapTOcontnav_srvs(map<string, string> &dictionarieMap_request_value,contnav_srvs::TABLE_CONTORLLER &tableContorllerService){

    map<string, string>::iterator iter_value;
    for(iter_value = dictionarieMap_request_value.begin(); iter_value != dictionarieMap_request_value.end(); iter_value++) {
        contnav_msgs::Dictionaries dictionaries;
        dictionaries.Key_i=iter_value->first;
        dictionaries.Value=iter_value->second;
        //  cout << " =========type==\"value\" || type==\"all\"======="  << endl;

        cout << iter_value->first << " : " << iter_value->second << endl;
        tableContorllerService.request.request_value_dictionariesVector.DictionariesVectors.push_back(dictionaries);
    }

}





//查询字典数据 查询所有的数据


void queryAllFromTABLE_DICTIONARES(){
    try {
        //查询字典数据
        SqlOperationClient.waitForExistence();
        contnav_srvs::TABLE_CONTORLLER tableContorllerService;
        tableContorllerService.request.request_type=TabeOperationType::selectSQLTABL;//selectSQLTABL：查询数据操作
        tableContorllerService.request.request_where_dictionariesVector.request_type=TabeName::TABLE_DICTIONARES;//要操作的数据库表
        tableContorllerService.request.isReturn_result_sqlStr=TabeOperationType::isReturn_result_sqlStr_true;//需要返回sql语句
        /**
              * 打开注释为按条件查询

          //1. where 条件
          whereSql("","Id","=","1",tableContorllerService);

             */

        //发送seervice
        SqlOperationClient.call(tableContorllerService);

    //    cout << "sql 语句" << " : " << tableContorllerService.response.result_sqlStr << endl;
        if (tableContorllerService.response.result!=ServiceResultType::ResultTypeOK)
            return;

        cout << " result_sqlStr : "  << tableContorllerService.response.result_sqlStr<<endl;
        cout << " result_count : "  << tableContorllerService.response.result_count<<endl;
        for (int l = 0; l < tableContorllerService.response.result_table_dictionares.size(); ++l) {

            contnav_msgs::TABLE_DICTIONARES table_dictionares =tableContorllerService.response.result_table_dictionares[l];

            dictionariesMap_[ table_dictionares.Key_i]= table_dictionares.Value;

            cout <<"Id: " <<table_dictionares.Id             <<", ";
            cout <<"TargetName: " <<table_dictionares.TargetName        <<", ";
            cout <<"Key_i: " << table_dictionares.Key_i       <<", ";
            cout <<"Value: " << table_dictionares.Value         <<", ";
            cout <<"Status: " << table_dictionares.Status    <<"; "    <<endl;

        }
        /*
        //遍历map,查看数据
        map<string, string>::iterator iter;
        for(iter = dictionariesMap_.begin(); iter != dictionariesMap_.end(); iter++) {
            cout << iter->first << " : " << iter->second << endl;
        }
       */
    }catch (exception e){
        cout<<  e.what()<<endl;
    }
}


//查询字典数据
void queryFromTABLE_DICTIONARESById(std::string id){
    try {
        //查询字典数据
        SqlOperationClient.waitForExistence();
        contnav_srvs::TABLE_CONTORLLER tableContorllerService;
        tableContorllerService.request.request_type=TabeOperationType::selectSQLTABL;//selectSQLTABL：查询数据操作
        tableContorllerService.request.request_where_dictionariesVector.request_type=TabeName::TABLE_DICTIONARES;//要操作的数据库表
        tableContorllerService.request.isReturn_result_sqlStr=TabeOperationType::isReturn_result_sqlStr_true;//需要返回sql语句

        //2.封装数据
        //1. where 条件
        whereSql("",TABLE_DICTIONARES_SQL_COLUMN_FIELD::Id,"=",id,tableContorllerService);


        //发送seervice
        SqlOperationClient.call(tableContorllerService);

  //      cout << "sql 语句" << " : " << tableContorllerService.response.result_sqlStr << endl;
        if (tableContorllerService.response.result!=ServiceResultType::ResultTypeOK)
            return;

        cout << " result_sqlStr : "  << tableContorllerService.response.result_sqlStr<<endl;
        cout << " result_count : "  << tableContorllerService.response.result_count<<endl;
        for (int l = 0; l < tableContorllerService.response.result_table_dictionares.size(); ++l) {

            dictionariesMap_[ tableContorllerService.response.result_table_dictionares[l].Key_i]= tableContorllerService.response.result_table_dictionares[l].Value;

        }
        /**
       //遍历map,查看数据
       map<string, string>::iterator iter;
       for(iter = dictionariesMap_.begin(); iter != dictionariesMap_.end(); iter++) {
           cout << iter->first << " : " << iter->second << endl;
       }
*/
   }catch (exception e){
       cout<<  e.what()<<endl;
   }
}


void operationFromTABLE_DICTIONARES_bySql(){
   try {

       //操作字典数据
        //   insert into TABLE_DICTIONARES (TargetName,Key_i,Value,Status)  values('场地编号','fieldNumber','00000','0');
       //    UPDATE TABLE_DICTIONARES SET  TargetName = '场地编号',Key_i = ''fieldNumber, Value = '00000', Status = '0'  WHERE Id = %d ;
       //    INSERT INTO TABLE_DICTIONARES (column1,column2,column3,...) VALUES (value1,value2,value3,...);
       //    delete from TABLE_DICTIONARES  WHERE Id = %d
       //    select * from  TABLE_DICTIONARES  ORDER BY Id
       SqlOperationClient.waitForExistence();
       contnav_srvs::TABLE_CONTORLLER tableContorllerService;

       //查询
       tableContorllerService.request.sqlStr=TabeOperationType::select+" * "+TabeOperationType::from  +TabeName::TABLE_DICTIONARES+TabeOperationType::WHERE+TABLE_DICTIONARES_SQL_COLUMN_FIELD::Key_i+" = "+TABLE_DICTIONARES_KEY::fieldName+ TabeOperationType::ORDER+ TabeOperationType::BY+ TABLE_DICTIONARES_SQL_COLUMN_FIELD::Id;
       tableContorllerService.request.request_type=TabeOperationType::selectSQLTABL;//selectSQLTABL：查询数据操作
       tableContorllerService.request.isReturn_result_sqlStr=TabeOperationType::isReturn_result_sqlStr_true;//需要返回sql语句
       //增加

       // tableContorllerService.request.sqlStr="insert into TABLE_DICTIONARES (TargetName,Key_i,Value,Status)  values('场地编号33','fieldNumber33','00033','0')";
       // tableContorllerService.request.request_type=TabeOperationType::insertSQLTABL;//insertSQLTABL：增加数据操作
       //更改
       // tableContorllerService.request.sqlStr="UPDATE TABLE_DICTIONARES SET  TargetName = '场地编号666',Key_i = 'fieldNumber66', Value = '00000', Status = '0'  WHERE Id = 16 ";
       // tableContorllerService.request.request_type=TabeOperationType::updateSQLTABL;//updateSQLTABL：更改数据操作
       //删除
       //   tableContorllerService.request.sqlStr="delete from TABLE_DICTIONARES  WHERE Id = 16";

       //要操作的数据库表
       tableContorllerService.request.request_where_dictionariesVector.request_type=TabeName::TABLE_DICTIONARES;
       SqlOperationClient.call(tableContorllerService);


     //  cout << "sql 语句" << " : " << tableContorllerService.response.result_sqlStr << endl;
       if (tableContorllerService.response.result!=ServiceResultType::ResultTypeOK)
           return;

       cout << " result_sqlStr : "  << tableContorllerService.response.result_sqlStr<<endl;
       cout << " result_count : "  << tableContorllerService.response.result_count<<endl;
       for (int l = 0; l < tableContorllerService.response.result_table_dictionares.size(); ++l) {

           dictionariesMap_[ tableContorllerService.response.result_table_dictionares[l].Key_i]= tableContorllerService.response.result_table_dictionares[l].Value;

       }

       /**
       //遍历map,查看数据
       map<string, string>::iterator iter;
       for(iter = dictionariesMap_.begin(); iter != dictionariesMap_.end(); iter++) {
           cout << iter->first << " : " << iter->second << endl;
       }

         */

    }catch (exception e){
        cout<<  e.what()<<endl;
    }
}

//=========================================================

//新增字典数据
void insertTABLE_DICTIONARES(std::string TargetName,std::string Key_i,std::string Value,std::string Status){
    try {
        //查询字典数据
        SqlOperationClient.waitForExistence();
        //1.创建消息
        contnav_srvs::TABLE_CONTORLLER tableContorllerService;
        tableContorllerService.request.request_type=TabeOperationType::insertSQLTABL;//insertSQLTABL：新增数据操作
        tableContorllerService.request.request_where_dictionariesVector.request_type=TabeName::TABLE_DICTIONARES;//要操作的数据库表
        tableContorllerService.request.isReturn_result_sqlStr=TabeOperationType::isReturn_result_sqlStr_true;//需要返回sql语句
        //2.封装数据
        //新增的值
        map<string, string> dictionarieMap_request_value;
        dictionarieMap_request_value[TABLE_DICTIONARES_SQL_COLUMN_FIELD::TargetName]=TargetName;
        dictionarieMap_request_value[TABLE_DICTIONARES_SQL_COLUMN_FIELD::Key_i]=Key_i;
        dictionarieMap_request_value[TABLE_DICTIONARES_SQL_COLUMN_FIELD::Value]=Value;
        dictionarieMap_request_value[TABLE_DICTIONARES_SQL_COLUMN_FIELD::Status]=Status;
        mapTOcontnav_srvs(dictionarieMap_request_value,tableContorllerService);



        //3.发送service服务
        SqlOperationClient.call(tableContorllerService);
    //    cout << "sql 语句" << " : " << tableContorllerService.response.result_sqlStr << endl;
        if (tableContorllerService.response.result!=ServiceResultType::ResultTypeOK)
            return;

        cout << " result_sqlStr : "  << tableContorllerService.response.result_sqlStr<<endl;
        cout << " result_count : "  << tableContorllerService.response.result_count<<endl;

    }catch (exception e){
        cout<<  e.what()<<endl;
    }
}


//修改字典数据
void updateFromTABLE_DICTIONARES(std::string id,std::string TargetName,std::string Key_i,std::string Value,std::string Status){
    try {

        //查询字典数据
        SqlOperationClient.waitForExistence();
        //1.创建消息
        contnav_srvs::TABLE_CONTORLLER tableContorllerService;
        tableContorllerService.request.request_type=TabeOperationType::updateSQLTABL;//updateSQLTABL：更新数据操作
        tableContorllerService.request.request_where_dictionariesVector.request_type=TabeName::TABLE_DICTIONARES;//要操作的数据库表
        tableContorllerService.request.isReturn_result_sqlStr=TabeOperationType::isReturn_result_sqlStr_true;//需要返回sql语句
        //2.封装数据
        //2.1. where 条件

        whereSql("",TABLE_DICTIONARES_SQL_COLUMN_FIELD::Id,"=",id,tableContorllerService);
        whereSql(TabeOperationType::AND,TABLE_DICTIONARES_SQL_COLUMN_FIELD::Status,"=","0",tableContorllerService);

        //2.2修改的值
        map<string, string> dictionarieMap_request_value;
        dictionarieMap_request_value[TABLE_DICTIONARES_SQL_COLUMN_FIELD::TargetName]=TargetName;
        dictionarieMap_request_value[TABLE_DICTIONARES_SQL_COLUMN_FIELD::Key_i]=Key_i;
        dictionarieMap_request_value[TABLE_DICTIONARES_SQL_COLUMN_FIELD::Value]=Value;
        dictionarieMap_request_value[TABLE_DICTIONARES_SQL_COLUMN_FIELD::Status]=Status;
        mapTOcontnav_srvs(dictionarieMap_request_value,tableContorllerService);

        //3.发送service服务
        SqlOperationClient.call(tableContorllerService);
        //cout << "sql 语句" << " : " << tableContorllerService.response.result_sqlStr << endl;
        if (tableContorllerService.response.result!=ServiceResultType::ResultTypeOK)
            return;

        cout << " result_sqlStr : "  << tableContorllerService.response.result_sqlStr<<endl;
        cout << " result_count : "  << tableContorllerService.response.result_count<<endl;

    }catch (exception e){
        cout<<  e.what()<<endl;
    }
}


//删除字典数据
void deleteFromTABLE_DICTIONARESById(std::string id){
    try {
        //查询字典数据
        SqlOperationClient.waitForExistence();
        //1.创建消息
        contnav_srvs::TABLE_CONTORLLER tableContorllerService;
        tableContorllerService.request.request_type=TabeOperationType::deleteSQLTABL;//deleteSQLTABL：删除数据操作
        tableContorllerService.request.request_where_dictionariesVector.request_type=TabeName::TABLE_DICTIONARES;//要操作的数据库表
        tableContorllerService.request.isReturn_result_sqlStr=TabeOperationType::isReturn_result_sqlStr_true;//需要返回sql语句
        //2.封装数据
        //1. where 条件
        whereSql("",TABLE_DICTIONARES_SQL_COLUMN_FIELD::Id,"=",id,tableContorllerService);

        //3.发送service服务
        SqlOperationClient.call(tableContorllerService);
    //    cout << "sql 语句" << " : " << tableContorllerService.response.result_sqlStr << endl;
        if (tableContorllerService.response.result!=ServiceResultType::ResultTypeOK)
            return;
        cout << " result_sqlStr : "  << tableContorllerService.response.result_sqlStr<<endl;
        cout << " result_count : "  << tableContorllerService.response.result_count<<endl;

    }catch (exception e){
        cout<<  e.what()<<endl;
    }
}
//===========================================================
//批量增加任务表数据
void insertSQLTABL_BATCH_TASK(){
    try {
        cout << " ===test================批量增加任务表数据=========================  "  <<endl;

        SqlOperationClient.waitForExistence();
        //1.创建消息
        contnav_srvs::TABLE_CONTORLLER tableContorllerService;
        tableContorllerService.request.request_type=TabeOperationType::insertSQLTABL_BATCH_TASK;//insertSQLTABL_BATCH_TASK 批量增加任务表数据
        tableContorllerService.request.request_where_dictionariesVector.request_type=TabeName::TABLE_TASKPOSE;//要操作的数据库表
        tableContorllerService.request.isCircular= true;//是否循环,false 默认新增一次数据;
        tableContorllerService.request.circular=2;//循环次数

        //2.新增数据
        tableContorllerService.request.request_TaskNames.push_back("第一1");
        tableContorllerService.request.request_MarkNames.push_back("设备部更改1");
        tableContorllerService.request.request_Messages.push_back("s1");
        tableContorllerService.request.request_Controllers.push_back("111");
        tableContorllerService.request.request_SocketObjectNames.push_back("socketObjectName1");

        tableContorllerService.request.request_TaskNames.push_back("第二1");
        tableContorllerService.request.request_MarkNames.push_back("设备部更改2");
        tableContorllerService.request.request_Messages.push_back("s2");
        tableContorllerService.request.request_Controllers.push_back("222");
        tableContorllerService.request.request_SocketObjectNames.push_back("socketObjectName1");


        tableContorllerService.request.request_TaskNames.push_back("第三1");
        tableContorllerService.request.request_MarkNames.push_back("设备部更改3");
        tableContorllerService.request.request_Messages.push_back("s3");
        tableContorllerService.request.request_Controllers.push_back("333");
        tableContorllerService.request.request_SocketObjectNames.push_back("socketObjectName1");


        tableContorllerService.request.request_TaskNames.push_back("第四1");
        tableContorllerService.request.request_MarkNames.push_back("设备部更改6");
        tableContorllerService.request.request_Messages.push_back("s4");
        tableContorllerService.request.request_Controllers.push_back("444");
        tableContorllerService.request.request_SocketObjectNames.push_back("socketObjectName1");


        //3.发送service服务
        SqlOperationClient.call(tableContorllerService);
        //    cout << "sql 语句" << " : " << tableContorllerService.response.result_sqlStr << endl;
        if (tableContorllerService.response.result!=ServiceResultType::ResultTypeOK)
            return;

        cout << " result_sqlStr : "  << tableContorllerService.response.result_sqlStr<<endl;
        cout << " result_count : "  << tableContorllerService.response.result_count<<endl;

    }catch (exception e){
        cout<<  e.what()<<endl;
    }
}



//删除任务表数据
void deleteFromTABLE_TASK(std::string id){
    try {
        //查询字典数据
        SqlOperationClient.waitForExistence();
        //1.创建消息
        contnav_srvs::TABLE_CONTORLLER tableContorllerService;
        tableContorllerService.request.request_type=TabeOperationType::deleteSQLTABL;//deleteSQLTABL：删除数据操作
        tableContorllerService.request.request_where_dictionariesVector.request_type=TabeName::TABLE_TASKPOSE;//要操作的数据库表
        tableContorllerService.request.isReturn_result_sqlStr=TabeOperationType::isReturn_result_sqlStr_true;//需要返回sql语句
        //2.封装数据
        //1. where 条件
       // whereSql("","Id","=",id,tableContorllerService);

        //3.发送service服务
        SqlOperationClient.call(tableContorllerService);
        //    cout << "sql 语句" << " : " << tableContorllerService.response.result_sqlStr << endl;
        if (tableContorllerService.response.result!=ServiceResultType::ResultTypeOK)
            return;
        cout << " result_sqlStr : "  << tableContorllerService.response.result_sqlStr<<endl;
        cout << " result_count : "  << tableContorllerService.response.result_count<<endl;

    }catch (exception e){
        cout<<  e.what()<<endl;
    }
}
// rosrun contnav testSqlite_node
int main(int argc,char *argv[]){
    //节点名
    string nodeName = "testSqlite_contnav_contnav_global_planner_node";
    //初始化节点
    ros::init(argc,argv,nodeName);
    //创建节点
    ros::NodeHandle node;
    //监控 ctrl c
    signal(SIGINT, mySigintHandler);


    //数据库操作
    SqlOperationClient = node.serviceClient<contnav_srvs::TABLE_CONTORLLER::Request,contnav_srvs::TABLE_CONTORLLER::Response>("/SqlOperation");






    //新增字典数据
//    string home = getenv("HOME");
//    insertTABLE_DICTIONARES("场地编号","fieldNumber","00000","0");
//    insertTABLE_DICTIONARES("场地名称","fieldName","EMPTY","0");
//    insertTABLE_DICTIONARES("机器人编号","robotNumber","wt_00001","0");
//    insertTABLE_DICTIONARES("当前地图名称","mapName","map","0");
//    insertTABLE_DICTIONARES("当前地图楼层","mapfloor","1","0");
//    insertTABLE_DICTIONARES("地图路径","mapPath",home+"/slam/map","0");
//    insertTABLE_DICTIONARES("建图状态","softMapState","false","0");
//    insertTABLE_DICTIONARES("软急停状态","softEstopState",TabeOperationType::isReturn_result_sqlStr_true,"0");
//    insertTABLE_DICTIONARES("当前建图算法","createMapAlgorithm","1","0");// 1.gmapping 2.cartographer 3.slamtobox
//    //mysql
//    insertTABLE_DICTIONARES("mysql数据库地址","mysqlIp","127.0.0.1","0");
//    insertTABLE_DICTIONARES("mysql数据库用户名","mysqluse","root","0");
//    insertTABLE_DICTIONARES("mysql数据库密码","mysqlpassword","root","0");
//    insertTABLE_DICTIONARES("mysql数据库名称","mysqlDatabaseName","TABLE_POSE","0");

//     insertTABLE_DICTIONARES("场地编号11","fieldNumber11","0000011","0");
//    insertTABLE_DICTIONARES("场地名称11","fieldName11","EMPTY11","0");
//   insertTABLE_DICTIONARES("机器人编号11","robotNumber11","wt_0000111","0");
//    insertTABLE_DICTIONARES("当前地图名称11","mapName11","map11","0");
//    insertTABLE_DICTIONARES("当前地图楼层11","mapfloor11","111","0");

    //修改字典数据
  //  updateFromTABLE_DICTIONARES("10","场地编号999","fieldNumber99","0000099","-1");

    //删除字典数据
 //  deleteFromTABLE_DICTIONARESById("14");


    //查询字典数据 查询所有的数据
   queryAllFromTABLE_DICTIONARES();
     //  operationFromTABLE_DICTIONARES_bySql();
    //  queryFromTABLE_DICTIONARESById("1");

//=========新增任务数据=======================
//   insertSQLTABL_BATCH_TASK();
 //  deleteFromTABLE_TASK("1");
    //事件轮询
    ros::spin();
    return 0;
}



